﻿package org.flixel
{
    import org.flixel.plugin.*;

    public class FlxTimer extends Object
    {
        public var time:Number;
        public var loops:uint;
        public var paused:Boolean;
        public var finished:Boolean;
        protected var _callback:Function;
        protected var _timeCounter:Number;
        protected var _loopsCounter:uint;

        public function FlxTimer()
        {
            this.time = 0;
            this.loops = 0;
            this._callback = null;
            this._timeCounter = 0;
            this._loopsCounter = 0;
            this.paused = false;
            this.finished = false;
            return;
        }// end function

        public function destroy() : void
        {
            this.stop();
            this._callback = null;
            return;
        }// end function

        public function update() : void
        {
            this._timeCounter = this._timeCounter + FlxG.elapsed;
            while (this._timeCounter >= this.time && !this.paused && !this.finished)
            {
                
                this._timeCounter = this._timeCounter - this.time;
                var _loc_1:String = this;
                var _loc_2:* = this._loopsCounter + 1;
                _loc_1._loopsCounter = _loc_2;
                if (this.loops > 0 && this._loopsCounter >= this.loops)
                {
                    this.stop();
                }
                if (this._callback != null)
                {
                    this._callback(this);
                }
            }
            return;
        }// end function

        public function start(param1:Number = 1, param2:uint = 1, param3:Function = null) : FlxTimer
        {
            var _loc_4:* = manager;
            if (manager != null)
            {
                _loc_4.add(this);
            }
            if (this.paused)
            {
                this.paused = false;
                return this;
            }
            this.paused = false;
            this.finished = false;
            this.time = param1;
            this.loops = param2;
            this._callback = param3;
            this._timeCounter = 0;
            this._loopsCounter = 0;
            return this;
        }// end function

        public function stop() : void
        {
            this.finished = true;
            var _loc_1:* = manager;
            if (_loc_1 != null)
            {
                _loc_1.remove(this);
            }
            return;
        }// end function

        public function get timeLeft() : Number
        {
            return this.time - this._timeCounter;
        }// end function

        public function get loopsLeft() : int
        {
            return this.loops - this._loopsCounter;
        }// end function

        public function get progress() : Number
        {
            if (this.time > 0)
            {
                return this._timeCounter / this.time;
            }
            return 0;
        }// end function

        public static function get manager() : TimerManager
        {
            return FlxG.getPlugin(TimerManager) as TimerManager;
        }// end function

    }
}
